<?php


  class Akrudo {
    
    public $logger;
    public $environment;
    public $config;
    private $_app_controller;
    
    static $error_levels =  array(
          'emerg' => PEAR_LOG_EMERG,
          'alert' => PEAR_LOG_ALERT,
          'crit' => PEAR_LOG_CRIT,
          'err' => PEAR_LOG_ERR,
          'warning' => PEAR_LOG_WARNING,
          'notice' => PEAR_LOG_NOTICE,
          'info' => PEAR_LOG_INFO,
          'debug' => PEAR_LOG_DEBUG);
    
    
  ### Controller/action methods
  
    
    static function if_compile_php () {
      return COMPILE_PHP;
    }
    
  #############################################################################################################################################################################################################
  
  
  
  ### Controller static methods
    
    
    public function load_controller () {
      self::require_controller($this->_request->controller);
      $code = '$this->app_controller = new ' . self::controller_class ($this->_request->controller) . '();';
      eval ($code);
      $this->app_controller->Params =& $this->request;
      self::raise_info($this->request);
      $this->app_controller->_request =& $this->_request;
      return $this->app_controller;
    }
    
    
    public function get_hanlder() {    
      if (Akrudo::if_compile_php()) {
        $path = PreCompiler::compile_file('config' . S . 'routes.php', ROOT_DIR);
      } else {
        $path = ROOT_DIR . ENV . S . 'config' . S . 'routes.php';
      }
      require_once($path);
      require_once(RUNTIME_DIR . 'GetHandler.php');
    }
  
  
    
    
    
    static function controller_class ($controller) {
      return $controller . '_controller';
    }
    
    
    # require 'controller/*_controller.php' file, doesn't perform action (see self::perform_controller_action())
    static function require_controller ($controller) {
      if (!in_array("{$controller}_controller", get_declared_classes())) {
        self::check_if_controller_exists($controller);
        if (self::if_compile_php()) {
          PreCompiler::compile_and_require_file(self::get_controller_path($controller));
          $method_name = 'render';
          $class_name = $controller . '_controller';  
          if (class_exists($class_name)) {
            classkit_method_add ($class_name, $method_name, '', "\$args = func_get_args();return parent::parent_{$method_name}('{$class_name}', 1, \$args[0]);");
          } else {
            self::raise_err("{$class_name} class not found in {$class_name}.php!");
          }
        } else {
          require_once(self::get_controller_root_path($controller));
        }
      }
    }
    
    # returns controller's root path in current ENV's directory
    static function get_controller_root_path ($controller) {
      return ROOT_DIR . self::get_controller_path($controller);
    }
    
    # returns controller's app path in current ENV's directory
    static function get_controller_path ($controller) {
      return APP_PREFIX . 'controller' . S . $controller . '_controller.php';
    }
    
    
    # triggers error if controller not found in current ENV's directory
    static function check_if_controller_exists($controller) {
      $controller_path = self::get_controller_root_path ($controller);
      if (!file_exists($controller_path)) {
        self::raise_err("No controller found. Make sure {$controller_path} exists.");
      } else {
        return true;
      }
    }
    
    
    
    
  #############################################################################################################################################################################################################
  
  
  ### View methods
    
    
    # returns view's root path depending on COMPILE_PHP constant
    static function get_include_view_root_path ($path) {
      if (self::if_compile_php()) {
      	return PreCompiler::compile_file(self::get_view_path($path));
      } else {
      	return ROOT_DIR . self::get_view_path($path);
      }
    }
    
    # returns view's root path in current ENV's directory
    static function get_view_root_path ($path) {
      return ROOT_DIR . self::get_view_path($path);
    }
    
    # returns view's app path in current ENV's directory
    static function get_view_path ($path) {
      return APP_PREFIX . 'view' . S . $path . '.pnml';
    }
    
    # triggers error if controller not found in current ENV's directory
    static function view_exists($view) {
      $view_path = self::get_view_root_path ($view);
      if (!file_exists($view_path)) {
        return false;
      } else {
        return true;
      }
    }
    
    

  #############################################################################################################################################################################################################
    
  ### Loads YAML config file into variable
    function load_config_file($name) {
      if (file_exists($filepath = CONFIG_DIR . $name . '.yaml')) {
        $this->config->$name = Spyc::YAMLLoad($filepath);
      } else {
        self::raise_crit("No " . $filepath . " specified!");
      }
    }
    
    
    
  #############################################################################################################################################################################################################
    
  ### Error raising / handling
    
    static function get_error_code ($name) {
      return self::$error_levels[$name];
    }
    static function raise_error($message, $code) {
      $error = new Error();
      $error->message = $message;
      $error->code = $code;
      $error->backtrace = debug_backtrace();
      Error::handle_error($error);
    }
    
    static function raise_debug($message) {
      self::raise_error ($message, PEAR_LOG_DEBUG);
    }
    
    static function raise_info($message) {
      self::raise_error ($message, PEAR_LOG_INFO);
    }
    
    static function raise_notice($message) {
      self::raise_error ($message, PEAR_LOG_NOTICE);
    }
    
    static function raise_warning($message) {
      self::raise_error ($message, PEAR_LOG_WARNING);
    }
    
    static function raise_err($message) {
      self::raise_error ($message, PEAR_LOG_ERR);
    }
    
    static function raise_crit($message) {
      self::raise_error ($message, PEAR_LOG_CRIT);
    }
    
    static function raise_alert($message) {
      self::raise_error ($message, PEAR_LOG_ALERT);
    }
    
    static function raise_emerg($message) {
      self::raise_error ($message, PEAR_LOG_EMERG);
    }
  
  }
  
?>